/*==========================================================================*\
 |  $Id: Import.java 9 2009-11-06 02:47:13Z VTWoods $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2009 Virginia Tech
 |
 |  This file is part of CloudSpace.
 |
 |  CloudSpace is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  CloudSpace is distributed in the hope that it will be useful,
 |  but WITHOUT ANY WARRANTY; without even the implied warranty of
 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 |  GNU General Public License for more details.
 |
 |  You should have received a copy of the GNU General Public License
 |  along with CloudSpace; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

package student.web;

import org.zkoss.zk.ui.Executions;

import student.web.internal.JavaStatementElement;

//-------------------------------------------------------------------------
/**
 * A ZUL import statement for Java classes.
 * <p>An &lt;import&gt; tag takes a single property.  For convenience, you
 * can spell this property in two different ways, depending on what makes
 * your code more readable:
 * </p>
 * <dl>
 * <dt><b>Parameters</b></dt>
 * <dd><code>class</code> - The name of a single Java class to import, or</dd>
 * <dd><code>classes</code> - The name of a Java package, plus ".*", to
 *   import all classes in a package</dd>
 * </dl>
 * <p>Examples:</p>
 * <pre>
 * &lt;!-- Import a single class --&gt;
 * &lt;import class="java.io.File"/&gt;
 *
 * &lt;!-- Import a whole package --&gt;
 * &lt;import classes="java.util.*"/&gt;
 * </pre>
 *
 * @author Stephen Edwards
 * @author Last changed by $Author: VTWoods $
 * @version $Revision: 9 $, $Date: 2009-11-06 02:47:13 +0000 (Fri, 06 Nov 2009) $
 */
public class Import
    extends JavaStatementElement
{
    //~ Constructor ...........................................................

    // ----------------------------------------------------------
    /**
     * Creates a new Import component.
     */
    public Import()
    {
        super();
    }


    //~ Public methods ........................................................

    // ----------------------------------------------------------
    /**
     * Mutator for the "class" property.  Note that there is no corresponding
     * accessor, since {@link #getClass()} performs a different (and
     * important!) function.
     * @param newClass The name of the Java class to import
     */
    public void setClass(String newClass)
    {
        setClasses(newClass);
    }


    // ----------------------------------------------------------
    /**
     * Accessor for the "classes" property.
     * @return The class pattern to be imported
     */
    public String getClasses()
    {
        return classes;
    }


    // ----------------------------------------------------------
    /**
     * Mutator for the "classes" property.
     * @param newClasses The Java package pattern to import (e.g.,
     * "some.package.*").
     */
    public void setClasses(String newClasses)
    {
        classes = newClasses;
        if (classes != null)
        {
            try
            {
                if (classes.indexOf("${") >= 0)
                {
                    classes = (String)Executions.evaluate(
                        this, classes, String.class);
                }
                getPage().getInterpreter( getPage().getZScriptLanguage() )
                .interpret( "import " + getClasses() + ";",
                            getParent().getNamespace() );
            }
            catch(Exception e)
            {
                student.web.internal.Exceptions.addSimpleExceptionGrid(
                    getParent(), e, "In &lt;import&gt; tag for \""
                    + classes + "\"");
            }
        }
    }


    //~ Instance/static variables .............................................

    private String classes;
}
